home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / ZPASSWD.PY < prev    next >
Encoding:
Python Source  |  2000-05-10  |  8.9 KB  |  254 lines

  1. #!/usr/bin/env python
  2. ##############################################################################
  3. # Zope Public License (ZPL) Version 1.0
  4. # -------------------------------------
  5. # Copyright (c) Digital Creations.  All rights reserved.
  6. # This license has been certified as Open Source(tm).
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. # 1. Redistributions in source code must retain the above copyright
  11. #    notice, this list of conditions, and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. #    notice, this list of conditions, and the following disclaimer in
  14. #    the documentation and/or other materials provided with the
  15. #    distribution.
  16. # 3. Digital Creations requests that attribution be given to Zope
  17. #    in any manner possible. Zope includes a "Powered by Zope"
  18. #    button that is installed by default. While it is not a license
  19. #    violation to remove this button, it is requested that the
  20. #    attribution remain. A significant investment has been put
  21. #    into Zope, and this effort will continue if the Zope community
  22. #    continues to grow. This is one way to assure that growth.
  23. # 4. All advertising materials and documentation mentioning
  24. #    features derived from or use of this software must display
  25. #    the following acknowledgement:
  26. #      "This product includes software developed by Digital Creations
  27. #      for use in the Z Object Publishing Environment
  28. #      (http://www.zope.org/)."
  29. #    In the event that the product being advertised includes an
  30. #    intact Zope distribution (with copyright and license included)
  31. #    then this clause is waived.
  32. # 5. Names associated with Zope or Digital Creations must not be used to
  33. #    endorse or promote products derived from this software without
  34. #    prior written permission from Digital Creations.
  35. # 6. Modified redistributions of any form whatsoever must retain
  36. #    the following acknowledgment:
  37. #      "This product includes software developed by Digital Creations
  38. #      for use in the Z Object Publishing Environment
  39. #      (http://www.zope.org/)."
  40. #    Intact (re-)distributions of any official Zope release do not
  41. #    require an external acknowledgement.
  42. # 7. Modifications are encouraged but must be packaged separately as
  43. #    patches to official Zope releases.  Distributions that do not
  44. #    clearly separate the patches from the original work must be clearly
  45. #    labeled as unofficial distributions.  Modifications which do not
  46. #    carry the name Zope may be packaged in any form, as long as they
  47. #    conform to all of the clauses above.
  48. # Disclaimer
  49. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  50. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  52. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  53. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  56. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  57. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  58. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  59. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60. #   SUCH DAMAGE.
  61. # This software consists of contributions made by Digital Creations and
  62. # many individuals on behalf of Digital Creations.  Specific
  63. # attributions are listed in the accompanying credits file.
  64. ##############################################################################
  65. """Zope password change system"""
  66.  
  67. __version__='$Revision: 1.10 $ '[11:-2]
  68.  
  69. import sys, string, sha, binascii, whrandom, getopt, getpass, os
  70.  
  71. try:
  72.     from crypt import crypt
  73. except ImportError:
  74.     crypt = None
  75.  
  76. def generate_salt():
  77.     """Generate a salt value for the crypt function."""
  78.     salt_choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  79.                    "abcdefghijklmnopqrstuvwxyz" \
  80.                    "0123456789./"
  81.     return whrandom.choice(salt_choices)+whrandom.choice(salt_choices)
  82.  
  83. def generate_passwd(password, encoding):
  84.     encoding=string.upper(encoding)
  85.     if encoding == 'SHA':
  86.         pw = '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1]
  87.     elif encoding == 'CRYPT':
  88.         pw = '{CRYPT}' + crypt(password, generate_salt())
  89.     elif encoding == 'CLEARTEXT':
  90.         pw = password
  91.  
  92.     return pw
  93.  
  94. def write_access(home, user='', group=''):
  95.     import whrandom
  96.     pw_choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  97.                  "abcdefghijklmnopqrstuvwxyz" \
  98.                  "0123456789!"
  99.  
  100.     ac_path=os.path.join(home, 'access')
  101.     if not os.path.exists(ac_path):
  102.         print '-'*78
  103.         print 'creating default access file'
  104.         acfile=open(ac_path, 'w')
  105.         pw = ''
  106.         for i in range(8):
  107.             pw = pw + whrandom.choice(pw_choices)
  108.         acfile.write('superuser:' + generate_passwd(pw, 'SHA'))
  109.         acfile.close()
  110.         os.system('chmod 644 access')
  111.  
  112.         print """Note:
  113.         The super user name and password are 'superuser'
  114.         and '%s'.
  115.  
  116.         You can change the superuser name and password with the
  117.         zpasswd script.  To find out more, type:
  118.  
  119.         %s zpasswd.py
  120.         """ % (pw, sys.executable)
  121.  
  122.         import do; do.ch(ac_path, user, group)
  123.  
  124. def main(argv):
  125.     short_options = ':u:p:e:d:'
  126.     long_options = ['username=',
  127.                     'password=',
  128.                     'encoding=',
  129.                     'domains=']
  130.  
  131.     usage = """%s [options] filename
  132.  
  133. If this program is called without command-line options, it will prompt
  134. for all necessary information.  The available options are:
  135.  
  136.     -u / --username=
  137.     Set the username to be used for the superuser
  138.  
  139.     -p / --password=
  140.     Set the password
  141.  
  142.     -e / --encoding=
  143.     Set the encryption/encoding rules.  Defaults to SHA-1. OPTIONAL
  144.  
  145.     -d / --domains=
  146.     Set the domain names that the user user can log in from.  Defaults to
  147.     any. OPTIONAL.
  148.     
  149.     Filename is required, and should be the name of the file to store the
  150.     information in (usually "access").
  151.     
  152. Copyright (C) 1999 Digital Creations, Inc.
  153. """ % argv[0]
  154.  
  155.     try:
  156.         if len(argv) < 2:
  157.             raise "CommandLineError"
  158.         
  159.         optlist, args = getopt.getopt(sys.argv[1:], short_options, long_options)
  160.  
  161.         if len(args) != 1:
  162.             raise "CommandLineError"
  163.  
  164.         access_file = open(args[0], 'w')
  165.  
  166.         if len(optlist) > 0:
  167.             # Set the sane defaults
  168.             username = 'superuser'
  169.             encoding = 'SHA'
  170.             domains = ''
  171.         
  172.             for opt in optlist:
  173.                 if (opt[0] == '-u') or (opt[0] == '--username'):
  174.                     username = opt[1]
  175.                 elif (opt[0] == '-p') or (opt[0] == '--password'):
  176.                     password = opt[1]
  177.                 elif (opt[0] == '-e') or (opt[0] == '--encoding'):
  178.                     encoding = opt[1]
  179.                 elif (opt[0] == '-d') or (opt[0] == '--domains'):
  180.                     domains = ":" + opt[1]
  181.  
  182.             # Verify that we got what we need
  183.             if not username or not password:
  184.                 raise "CommandLineError"
  185.  
  186.             access_file.write(username + ':' +
  187.                               generate_passwd(password, encoding) +
  188.                               domains)
  189.  
  190.         else:
  191.             # Run through the prompts
  192.             while 1:
  193.                 username = raw_input("Username: ")
  194.                 if username != '':
  195.                     break
  196.                
  197.             while 1:
  198.                 password = getpass.getpass("Password: ")
  199.                 verify = getpass.getpass("Verify password: ")
  200.                 if verify == password:
  201.                     break
  202.                 else:
  203.                     password = verify = ''
  204.                     print "Password mismatch, please try again..."
  205.  
  206.             while 1:
  207.                 print """
  208. Please choose a format from:
  209.  
  210. SHA - SHA-1 hashed password
  211. CRYPT - UNIX-style crypt password
  212. CLEARTEXT - no protection.
  213. """
  214.                 encoding = raw_input("Encoding: ")
  215.                 if encoding != '':
  216.                     break
  217.  
  218.             domains = raw_input("Domain restrictions: ")
  219.             if domains: domains = ":" + domains
  220.  
  221.             access_file.write(username + ":" +
  222.                       generate_passwd(password, encoding) +
  223.                       domains)
  224.             
  225.     except "CommandLineError":
  226.         sys.stderr.write(usage)
  227.         sys.exit(1)
  228.  
  229.     
  230. # If called from the command line
  231. if __name__=='__main__': main(sys.argv)
  232.  
  233.